package com.hgc.ch01.sync;

/**
 * Created by 01367627 on 2019/7/5.
 */
public class SyncInteger {

    private static class MyRunnable implements Runnable{
        private Integer i = 1;
        private Object o = new Object();

        public void run() {
            System.out.println(Thread.currentThread().getName() + " is running");
            synchronized (i){
                System.out.println(Thread.currentThread().getName() + "get the lock:" + i.hashCode());
                i ++;
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "run over.");
            }
        }
    }

    public static void main(String[] args) {
        MyRunnable runnable = new MyRunnable();
        for(int i = 0;i<5;i++){
            Thread t = new Thread(runnable);
            t.setName("t" + (i+1));
            t.start();
        }
    }


}
